package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.PointDao;
import com.coding.dao.TaskDao;
import com.coding.dao.TasklistDao;
import com.coding.dao.UserDao;
import com.coding.pojo.Point;
import com.coding.pojo.Task;
import com.coding.pojo.Tasklist;
import com.coding.pojo.User;
import com.coding.service.TasklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/26
 **/
@Service
public class TasklistImpl implements TasklistService {
    @Autowired
    TasklistDao tasklistDao;
    @Autowired
    TaskDao taskDao;
    @Autowired
    UserDao userDao;
    @Autowired
    PointDao pointDao;

    @Override
    public void getlistbyid(int user_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        List<Tasklist> tasklists = tasklistDao.selectByuser(user_id);
        if (tasklists.size() > 0) {
            baseModel.setSucceed("获取用户任务列表成功", tasklists);
        } else {
            List<Task> tasks=taskDao.queryAlllist(new Task());
            List<Tasklist> newTaskList=new ArrayList<>();
            if (tasks!=null){
                for (Task task:tasks
                     ) {
                    Tasklist tasklist=new Tasklist();
                    tasklist.setUser_id(user_id);
                    tasklist.setTask_id(task.getTask_id());
                    tasklist.setTasklist_time(DateUtil.getStringDate());
                    tasklist.setTasklist_status(0);
                    tasklistDao.insertSelective(tasklist);
                    tasklist.setTask(task);

                    newTaskList.add(tasklist);
                }
                baseModel.setSucceed("成功初始化用户任务列表",newTaskList);
            }else {
                baseModel.setFailure("获取用户任务列表失败");
            }

        }

    }

    @Override
    public void finishtask(int user_id, int task_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Tasklist more=new Tasklist();
        more.setTask_id(task_id);
        more.setUser_id(user_id);
        Tasklist testTask=tasklistDao.selectByPrimaryKey(more);
        if (testTask==null){
            baseModel.setFailure("未查到到该任务");
            return;
        }
        Tasklist test=new Tasklist();
        test.setTask_id(task_id);
        test.setUser_id(user_id);
        Tasklist lastTasklist=tasklistDao.selectByPrimaryKey(test);

        if (lastTasklist.getTasklist_status()==1){
            baseModel.setSucceed("用户今日已完成该任务",lastTasklist);
            return;
        }
        Tasklist tasklist = new Tasklist();
        //完成任务
        tasklist.setUser_id(user_id);
        tasklist.setTask_id(task_id);
        tasklist.setTasklist_status(1);
        tasklist.setTasklist_time(DateUtil.getStringDate());
        tasklistDao.updateByPrimaryKeySelective(tasklist);
        if (tasklist.getTasklist_status() == 1) {
            Task task = taskDao.selectByPrimaryKey(tasklist.getTask_id());
            if (userDao.adduserpoint(user_id, task.getTask_point()) == 1) {
                Point point = new Point();
                point.setPoint_task_id(tasklist.getTask_id());
                point.setPoint_number(task.getTask_point());
                point.setPoint_user_id(tasklist.getUser_id());
                point.setPoint_name(task.getTask_name());
                point.setPoint_time(tasklist.getTasklist_time());
                if (pointDao.insertSelective(point) == 1) {
                    baseModel.setSucceed("成功完成任务,且用户" + user.getUser_iphone() + "获得" + task.getTask_point() + "点积分", tasklist);
                } else {
                    baseModel.setFailure("完成任务失败");
                }
            } else {
                baseModel.setSucceed("成功完成任务", tasklist);
            }
        } else {
            baseModel.setFailure("完成任务失败");
        }
    }

    @Override
    public void finishtask(String user_account, int task_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuser(user_account);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Tasklist tasklist = new Tasklist();
        if (user.getUser_id() != 0) {
            tasklist.setUser_id(user.getUser_id());
            tasklist.setTask_id(task_id);
            tasklist.setTasklist_status(1);
            tasklist.setTasklist_time(DateUtil.getStringDate());
        } else {
            baseModel.setFailure("查无用户" + user_account);
            return;
        }
        tasklistDao.updateByPrimaryKeySelective(tasklist);
        if (tasklist.getTasklist_status() == 1) {
            Task task = taskDao.selectByPrimaryKey(tasklist.getTask_id());
            if (userDao.adduserpoint(user.getUser_id(), task.getTask_point()) == 1) {
                Point point = new Point();
                point.setPoint_task_id(tasklist.getTask_id());
                point.setPoint_number(tasklist.getTask().getTask_point());
                point.setPoint_user_id(tasklist.getUser_id());
                point.setPoint_time(tasklist.getTasklist_time());
                if (pointDao.insertSelective(point) == 1) {
                    baseModel.setSucceed("成功完成任务,且用户" + user.getUser_iphone() + "获得" + task.getTask_point() + "点积分", tasklist);
                } else {
                    baseModel.setFailure("完成任务失败");
                }

            } else {
                baseModel.setSucceed("成功完成任务", tasklist);
            }
        } else {
            baseModel.setFailure("完成任务失败");
        }
    }

    @Override
    public void inserttasklist(int user_id, int task_id, String more, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Tasklist tasklist = new Tasklist();
        tasklist.setUser_id(user_id);
        tasklist.setTask_id(task_id);
        tasklist.setTasklist_more1(more);
        if (tasklistDao.selectByKey(tasklist) == 1) {
            baseModel.setFailure("用户已有该任务");
            return;
        }
        if (tasklistDao.insertSelective(tasklist) == 1) {

            baseModel.setSucceed("新增任务成功", tasklist);
        } else {
            baseModel.setFailure("新增任务失败");
        }
    }

    @Override
    public void queryall(Tasklist tasklist, BaseModel baseModel) throws Exception {
        List<Tasklist> tasklists = tasklistDao.queryAll(tasklist);
        if (tasklists.size() >= 0) {
            baseModel.setSucceed("成功获得对应的任务列表", tasklists);
        } else {
            baseModel.setFailure("获得对应任务列表失败");
        }
    }

    @Override
    public void queryDayFinish(int user_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }

        List<Tasklist> tasklists = tasklistDao.selectFinish(user_id);
        if (tasklists.size() > 0) {
            baseModel.setSucceed("获取用户完成任务列表成功", tasklists);
        } else {
            baseModel.setFailure("获取用户完成任务列表失败");
        }
    }
}
